package com.zhouyu.lifeCycle;

import com.zhouyu.service.User;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 * copyright (C), 2023,北京同创永益科技发展有限公司
 *
 * @author xiaodi
 * @version 1.0.0
 * <author>                     <time>                 <version>                   <description>
 * xiaodi               2023/12/3 12:07           1.0.0                      测试bean的生命周期
 * @program spring
 * @description 测试bean的生命周期
 * @create 2023/12/3 12:07
 */
@Component
public class TestLifeCycle implements EnvironmentAware, EmbeddedValueResolverAware, Ordered, DisposableBean, InitializingBean {

	private static Environment environment;

	@TestLifeCycleInject("TestLifeCycleInject")
	public String customField;
	String address;
	@Value("${zhouyu:123}")
	private String name;

	@Autowired
	private User user;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCustomField() {
		return customField;
	}

	public void setCustomField(String customField) {
		this.customField = customField;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * Invoked by the containing {@code BeanFactory} on destruction of a bean.
	 *
	 * @throws Exception in case of shutdown errors. Exceptions will get logged
	 *                   but not rethrown to allow other beans to release their resources as well.
	 */
	@Override
	public void destroy() throws Exception {
		System.out.println("6、销毁");
	}

	/**
	 * Invoked by the containing {@code BeanFactory} after it has set all bean properties
	 * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
	 * <p>This method allows the bean instance to perform validation of its overall
	 * configuration and final initialization when all bean properties have been set.
	 *
	 * @throws Exception in the event of misconfiguration (such as failure to set an
	 *                   essential property) or if initialization fails for any other reason
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("3、属性设置后");
	}

	/**
	 * Set the StringValueResolver to use for resolving embedded definition values.
	 *
	 * @param resolver
	 */
	@Override
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		String zhouyu = resolver.resolveStringValue("zhouyu");
		System.out.println(zhouyu);
	}

	/**
	 * Set the {@code Environment} that this component runs in.
	 *
	 * @param environment
	 */
	@Override
	public void setEnvironment(Environment environment) {
		String zhouyu = environment.getProperty("zhouyu");
		System.out.println(zhouyu);
		TestLifeCycle.environment = environment;
	}

	/**
	 * Get the order value of this object.
	 * <p>Higher values are interpreted as lower priority. As a consequence,
	 * the object with the lowest value has the highest priority (somewhat
	 * analogous to Servlet {@code load-on-startup} values).
	 * <p>Same order values will result in arbitrary sort positions for the
	 * affected objects.
	 *
	 * @return the order value
	 * @see #HIGHEST_PRECEDENCE
	 * @see #LOWEST_PRECEDENCE
	 */
	@Override
	public int getOrder() {
		return 0;
	}
}
